// packageG/mycomponent/cloud_shop/special_subject/special_subject.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null
    },
    page_id: {
      type: null
    },
    index: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    page: 1,
    total_page: 0,
    isLoadMore: false,
    dataList: []
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      })
      this.setDataM();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    tolink(e) {
      let {
        url,
        params
      } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `${url}${params ? params : ""}`,
      });
    },
    toBrand(e) {
      wx.navigateTo({
        url: '/mircoApp/cloud_shop/brand_home/brand_home?id=' + e.currentTarget.dataset.id,
      })
    },
    async getMoreData() {
      if (!this.data.isLoadMore) return
      this.data.isLoadMore = false;
      if (this.data.page >= this.data.total_page) return;
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.decorate.brandList"),
        data: {
          page: this.data.page
        }
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          dataList: this.data.dataList.concat(data.data.list.data)
        })
      } else {
        app.tips(data.msg);
        this.setData({
          page: this.data.page - 1
        })
      }
    },
    async setDataM() {
      this.data.dataList = this.data.datas.list;
      if (this.data.datas.get_style == 0) {
        // 装修获取全部
        let {
          data
        } = await app._postNetWork({
          url: app.getNetAddresss("plugin.cloud-shop.frontend.decorate.brandList")
        })
        if (data.result) {
          this.setData({
            dataList: data.data.list.data,
            total_page: data.data.list.last_page,
            page: 1,
            isLoadMore: true
          })
        } else {
          app.tips(data.msg);
        }
      } else {
        let ids = this.data.datas.list.map(ele => ele.id);
        if(ids.length <= 0) return
        let {
          data
        } = await app._postNetWork({
          url: app.getNetAddresss("plugin.cloud-shop.frontend.decorate.brandList"),
          data: {
            brand_ids: ids
          }
        })
        if (data.result) {
          this.data.isLoadMore = true;
          const idToGoodsMap = {};
          let arrData = data.data.list;
          arrData.forEach(item => {
            idToGoodsMap[item.id] = item.has_many_goods;
          });
          // 更新 this.datas.list 中对应 id 的数据
          this.data.dataList.forEach(item => {
            if (idToGoodsMap[item.id]) {
              item['has_many_goods'] = idToGoodsMap[item.id]
            } else {
              item['hidden'] = true
            }
          });
          this.setData({
            dataList: this.data.dataList
          })
        } else {
          app.tips(data.msg);
        }
      }

    }
  }
})